/*
 * 阔地教育科技有限公司版权所有(codyy.com/codyy.cn)
 * Copyright (c) 2019, Codyy and/or its affiliates. All rights reserved.
 */

package com.eachann.privacypolicy.library


import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Point
import android.os.Bundle
import android.view.*
import androidx.annotation.DrawableRes
import androidx.annotation.Nullable
import androidx.annotation.StringRes
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

/**
 * 隐私政策 dialog
 * created by eachann on 2020/04/14
 * [android.content.Context] 必须继承于[androidx.fragment.app.FragmentActivity]或[androidx.fragment.app.Fragment]
 * 用于获取[androidx.fragment.app.FragmentManager],否则无法正常实例化窗口
 */
class PrivacyPolicyDialog : DialogFragment() {
    private var controller = PrivacyPolicyController()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_FRAME, R.style.TransparentDialog)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return controller.getView()
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val di = super.onCreateDialog(savedInstanceState)
        di.requestWindowFeature(Window.FEATURE_NO_TITLE)
        controller.setDialog(di)
        return di
    }

    override fun onStart() {
        super.onStart()
        dialog?.let {
            val window = it.window
            if (window != null) {
                val lp = window.attributes
                window.setGravity(Gravity.CENTER)
                lp.copyFrom(window.attributes)
                lp.width =
                    getFitWidth() - resources.getDimensionPixelSize(R.dimen.privacy_policy_dialog_margin) * 2
                lp.height = (lp.width * 380f / 285).toInt()
                window.attributes = lp
            }
        }
    }

    private fun getFitWidth(): Int {
        val wm =
            context?.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val point = Point()
        wm.defaultDisplay.getSize(point)
        return point.x
    }

    override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
        controller.mOnCancelListener?.onCancel(dialog)
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        controller.mOnDismissListener?.onDismiss(dialog)
    }

    class Builder @JvmOverloads constructor(
        private val context: Context,
        private val tag: String? = null
    ) {
        private val p: PrivacyPolicyController.ModalParams =
            PrivacyPolicyController.ModalParams(context)

        fun setTitle(@StringRes titleId: Int): Builder {
            p.title = p.context.getText(titleId)
            return this
        }

        fun setTitle(@Nullable title: CharSequence): Builder {
            p.title = title
            return this
        }

        fun setMessage(@StringRes messageId: Int): Builder {
            p.message = p.context.getText(messageId)
            return this
        }

        fun setMessage(@Nullable message: CharSequence): Builder {
            p.message = message
            return this
        }

        fun setPositiveButton(@StringRes textId: Int, listener: DialogInterface.OnClickListener): Builder {
            p.positiveButtonText = p.context.getText(textId)
            p.positiveButtonListener = listener
            return this
        }

        fun setPositiveButtonBackground(@DrawableRes resid: Int): Builder {
            p.positiveButtonBg = resid
            return this
        }

        fun setPositiveButton(
            text: CharSequence,
            listener: DialogInterface.OnClickListener
        ): Builder {
            p.positiveButtonText = text
            p.positiveButtonListener = listener
            return this
        }

        fun setNeutralButton(@StringRes textId: Int, listener: DialogInterface.OnClickListener): Builder {
            p.neutralButtonText = p.context.getText(textId)
            p.neutralButtonListener = listener
            return this
        }

        fun setNeutralButton(
            text: CharSequence,
            listener: DialogInterface.OnClickListener
        ): Builder {
            p.neutralButtonText = text
            p.neutralButtonListener = listener
            return this
        }

        fun setCancelable(cancelable: Boolean): Builder {
            p.cancelable = cancelable
            return this
        }

        fun setOncancelListener(onCancelListener: DialogInterface.OnCancelListener): Builder {
            p.onCancelListener = onCancelListener
            return this
        }

        fun setOnDismissListener(onDismissListener: DialogInterface.OnDismissListener): Builder {
            p.onDismissListener = onDismissListener
            return this
        }

        fun create(): PrivacyPolicyDialog {
            val dialog = PrivacyPolicyDialog()
            p.apply(dialog.controller)
            dialog.isCancelable = p.cancelable
            return dialog
        }

        fun show() {
            when (context) {
                is FragmentActivity -> {
                    create().show(context.supportFragmentManager, tag)
                }
                is Fragment -> {
                    create().show(context.childFragmentManager, tag)
                }
                else -> {
                    throw IllegalArgumentException("context type must be FragmentManager")
                }
            }

        }

    }
}